package com.miao.hong.fang.base.dao;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.miao.hong.fang.util.SpringBeansHolder;

public class BaseDAO extends HibernateDaoSupport{

	private JdbcTemplate jdbcTemplate;
	
	protected JdbcTemplate getJdbcTemplate() {
		
		if (jdbcTemplate == null) {
			jdbcTemplate = SpringBeansHolder.getInstance().getJdbcTemplate();
		}
		
		return jdbcTemplate;
	}
	
	public String save(Object o) {
		return (String) getHibernateTemplate().save(o);
	}
	
	protected void removeFromCache(String sql, Class clazz) {
		List<String> ids = getJdbcTemplate().queryForList(sql, String.class);
		for(String id: ids) {
			getHibernateTemplate().getSessionFactory().evict(clazz, id);
		}
	}
}
